export default function tableFactory({ defaultFiltersRaw = [], defaultParamsRaw = {}, config = {}, changeColumn = [] }) {
  return {
    name: 'Table',
    props: {
      filtersRaw: {
        type: Array,
        default: () => []
      },
      paramsRaw: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        defaultTableConfig: {
          interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerChange/page`,
          hideSearchBar: true,
          tabIndex: 1,
          buttonGroupCode: 'default',
          apiHeader: {
            'Permission-PerCode': 'archivechange',
            'Permission-App': 'slm'
          },
          setting: [
            {
              prop: 'code',
              label: '单号',
              className: 'btn-green',
              minWidth: 140,
              isButton: true,
              clickFun: this.handleClickOrderCode
            },
            {
              prop: 'customerCode',
              label: '客户编码',
              minWidth: 130
            },
            {
              prop: 'customerName',
              label: '客户名称',
              minWidth: 100
            },
            {
              prop: 'officeName',
              label: '所属办事处',
              minWidth: 140
            },
            {
              prop: 'type',
              label: '变更类型',
              minWidth: 100,
              propEnum: {
                appCode: 'slm',
                domainCode: 'marketing',
                dictTypeCode: 'CTM_CUS_ARCHIVE_CHANGE_TYPE'
              }
            },
            {
              prop: 'submitBy',
              label: '变更申请人',
              minWidth: 100,
              formatData: row => {
                if (row.submitBy === undefined) {
                  return ''
                }
                return `${row.submitByName}(${row.submitBy})`
              }
            },
            {
              prop: 'submitTime',
              label: '变更申请时间',
              minWidth: 140
            },
            {
              prop: 'schedule',
              label: '变更进度',
              minWidth: 100,
              propEnum: {
                appCode: 'slm',
                domainCode: 'marketing',
                dictTypeCode: 'CTM_CUS_ARCHIVE_CHANGE_PROGRESS'
              }
            },
            // {
            //   prop: 'reviewResult',
            //   label: '审核结果',
            //   minWidth: 100,
            //   propEnum: {
            //     appCode: 'slm',
            //     domainCode: 'marketing',
            //     dictTypeCode: 'REVIEW_RESULT'
            //   }
            // },
            {
              prop: 'applyNo',
              label: 'BPM流程单号',
              minWidth: 140
            }
          ].map(e => {
            const t = changeColumn.find(ee => ee.prop === e.prop)
            if (t) {
              return {
                ...e,
                ...t
              }
            }
            return e
          }),
          btnList: null
        },
        selectedRows: []
      }
    },
    computed: {
      otherFilters() {
        return [...this.filtersRaw, ...defaultFiltersRaw]
      },
      otherRaw() {
        return { ...this.paramsRaw, ...defaultParamsRaw }
      },
      tableConfig() {
        return this.mergeConfig()
      }
    },
    render(h) {
      return h('table-box', {
        props: {
          noRequest: false,
          showIndex: true,
          ...(this.$attrs || {}),
          otherFilters: this.otherFilters,
          otherRaw: this.otherRaw,
          tableConfig: this.tableConfig
        },
        on: {
          'selection-change': this.handleSelectionChange,
          btnOperate: this.handleBtnOperate,
          'row-click': this.handleRowClick
        },
        ref: 'tableBox'
      })
    },
    created() {
    },
    methods: {
      mergeConfig() {
        for (const [k, v] of Object.entries(config)) {
          if (k !== 'setting') {
            this.$set(this.defaultTableConfig, k, v)
          } else if (v instanceof Array) {
            const setting = this.defaultTableConfig.setting || []
            for (const item of v) {
              const i = setting.findIndex(e => e.prop === item.prop)
              if (i >= 0) {
                setting.splice(i, 1, item)
              } else if (item.beforeProp) {
                const _i = setting.findIndex(e => e.prop === item.beforeProp)
                if (_i >= 0) {
                  setting.splice(_i, 1, setting[_i], item)
                }
              } else {
                setting.push(item)
              }
            }
          }
        }
        return this.defaultTableConfig
      },
      initTableData(flag) {
        this.$nextTick(() => {
          this.selectedRows = []
          this.$refs.tableBox && this.$refs.tableBox.initTableData(flag)
        })
      },
      refreshTableHeight() {
        this.$nextTick(() => {
          this.$refs.tableBox && this.$refs.tableBox.refreshTableHeight()
        })
      },
      handleSelectionChange(rows) {
        this.selectedRows = rows
      },
      handleBtnOperate(flag) {
        this.$emit('btnOperate', flag, this.selectedRows)
        this.$emit(flag, this.selectedRows)
      },
      handleRowClick({ row, event, colmn }) {
        this.selectedRows = [row]
        this.$emit('row-click', row, event, colmn)
      },
      handleClickOrderCode(row) {
        this.$emit('on-detail', row)
      }
    }
  }
}
